﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;

namespace Cobit5.BIZ.Cobit
{
    public class GoalProcess
    {
        #region Model
        private int _id;
        private string _description;
        private string _descriptionchn;
        private string _relatedmetric;
        private string _relatedmetricchn;
        /// <summary>
        /// 
        /// </summary>
        public int id
        {
            set { _id = value; }
            get { return _id; }
        }
        /// <summary>
        /// 
        /// </summary>
        public string description
        {
            set { _description = value; }
            get { return _description; }
        }
        /// <summary>
        /// 
        /// </summary>
        public string descriptionCHN
        {
            set { _descriptionchn = value; }
            get { return _descriptionchn; }
        }
        /// <summary>
        /// 
        /// </summary>
        public string relatedMetric
        {
            set { _relatedmetric = value; }
            get { return _relatedmetric; }
        }
        /// <summary>
        /// 
        /// </summary>
        public string relatedMetricCHN
        {
            set { _relatedmetricchn = value; }
            get { return _relatedmetricchn; }
        }
        #endregion Model 

        /// <summary>
        /// Get a GoalProcess by id
        /// </summary>
        public GoalProcess(int in_id)
        {
            if (GoalProcess.ExistGoalProcess(in_id) == 0)
                return;

            Model.Goal_Process_Related myGPR = new Model.Goal_Process_Related();
            DAL.Goal_Process_Related optGPR = new DAL.Goal_Process_Related();
            myGPR = optGPR.GetModel(in_id);

            this._id = myGPR.id;
            this._description = myGPR.description;
            this._descriptionchn = myGPR.descriptionCHN;
            this._relatedmetric = myGPR.relatedMetric;
            this._relatedmetricchn = myGPR.relatedMetricCHN;
        }

        /// <summary>
        /// Count all the GoalProcess
        /// </summary>
        public static int CountAllGoalProcess()
        {
            DAL.Goal_Process_Related optGPR = new DAL.Goal_Process_Related();
            return optGPR.GetRecordCount("1=1");
        }

        /// <summary>
        /// Return all GoalProcess
        /// </summary>
        public static List<GoalProcess> GetAllGoalProcess(string in_orderby, int in_startIndex, int in_endIndex)
        {
            DAL.Goal_Process_Related optGPR = new DAL.Goal_Process_Related();
            DataSet ds = optGPR.GetListByPage("1=1", in_orderby, in_startIndex, in_endIndex);
            
            /*** Goal Process Not Exist ***/
            if (ds == null || ds.Tables[0].Rows.Count < 1)
                return null;

            List<GoalProcess> returnListGoalProcess = new List<GoalProcess>();
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                GoalProcess newGoalProcess = new GoalProcess(int.Parse(ds.Tables[0].Rows[i]["id"].ToString()));
                returnListGoalProcess.Add(newGoalProcess);
            }
            
            return returnListGoalProcess;
        }

        /// <summary>
        /// Check if Exist this GoalProcess
        /// 1.Exist
        /// 0.Not existed
        /// </summary>
        public static int ExistGoalProcess(int in_id)
        {
            DAL.Goal_Process_Related optGPR = new DAL.Goal_Process_Related();
            bool ok= optGPR.Exists(in_id);
            if (ok) return 1;
            else return 0;
        }

        /// <summary>
        /// Create a new GoalProcess
        /// 1.Success
        /// 2.Id already existed
        /// 3.DAL Error
        /// </summary>
        public static int CreateGoalProcess(int in_id, string in_description, string in_descriptionCHN, string in_relatedMetric, string in_relatedMetricCHN)
        {
            if (GoalProcess.ExistGoalProcess(in_id) == 1)
                return 2;

            Model.Goal_Process_Related myGPR = new Model.Goal_Process_Related();
            myGPR.id = in_id;
            myGPR.description = in_description;
            myGPR.descriptionCHN = in_descriptionCHN;
            myGPR.relatedMetric = in_relatedMetric;
            myGPR.relatedMetricCHN = in_relatedMetricCHN;

            DAL.Goal_Process_Related optGPR = new DAL.Goal_Process_Related();
            bool ok= optGPR.Add(myGPR);
            if (!ok) return 3;
            else return 1;
        }

        /// <summary>
        /// Update a old GoalProcess
        /// 1.Success
        /// 2.Id not exists
        /// 3.DAL Error
        /// </summary>
        public int UpdateGoalProcess()
        {
            int in_id = this._id;
            string in_description = this._description;
            string in_descriptionCHN = this._descriptionchn;
            string in_relatedMetric = this._relatedmetric;
            string in_relatedMetricCHN = this._relatedmetricchn;

            if (GoalProcess.ExistGoalProcess(in_id) == 0)
                return 2;

            Model.Goal_Process_Related myGPR = new Model.Goal_Process_Related();
            myGPR.id = in_id;
            myGPR.description = in_description;
            myGPR.descriptionCHN = in_descriptionCHN;
            myGPR.relatedMetric = in_relatedMetric;
            myGPR.relatedMetricCHN = in_relatedMetricCHN;

            DAL.Goal_Process_Related optGPR = new DAL.Goal_Process_Related();
            bool ok = optGPR.Update(myGPR);
            if (!ok) return 3;
            else return 1;
        }

        /// <summary>
        /// Delete a useless GoalProcess
        /// 1.Success
        /// 2.Id not exists
        /// 3.Relationship deleting error
        /// 4.DAL Error
        /// </summary>
        public static int DeleteGoalProcess(int in_id)
        {
            if (GoalProcess.ExistGoalProcess(in_id) == 0)
                return 2;

            /*** Delete all the connection with Process ***/
            DAL.Process_Goal_Process optPGP = new DAL.Process_Goal_Process();
            DataSet ds = optPGP.GetList("goal_Process='" + in_id.ToString() + "'");
            if (ds != null || ds.Tables[0].Rows.Count > 0)
            {
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    try
                    {
                        optPGP.Delete(ds.Tables[0].Rows[i]["processID"].ToString(), int.Parse(ds.Tables[0].Rows[i]["goal_Process"].ToString()));
                    }
                    catch
                    {
                        return 3;
                    }
                }
            }

            DAL.Goal_Process_Related optGPR = new DAL.Goal_Process_Related();
            bool ok = optGPR.Delete(in_id);
            if (!ok) return 4;
            else return 1;
        }

        /// <summary>
        /// Count how many Process this GoalProcess has
        /// </summary>
        public int CountProcess()
        {
            DAL.Process_Goal_Process optPGP = new DAL.Process_Goal_Process();
            DataSet ds = optPGP.GetList("goal_Process='" + this._id + "'");

            if (ds != null || ds.Tables[0].Rows.Count == 0)
                return 0;
            else
                return ds.Tables[0].Rows.Count;
        }

        /// <summary>
        /// Return the list of all Processes
        /// </summary>
        public List<Process> Process()
        {
            DAL.Process_Goal_Process optPGP = new DAL.Process_Goal_Process();
            DataSet ds = optPGP.GetList("goal_Process='" + this._id + "' order by processID");

            if (ds != null || ds.Tables[0].Rows.Count == 0)
                return null;

            List<Process> myListProcess = new List<Process>();
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                Cobit.Process myProcess = new Process(ds.Tables[0].Rows[i]["processID"].ToString());
                myListProcess.Add(myProcess);
            }

            return myListProcess;
        }

        /// <summary>
        /// Add a process to the GoalProcess
        /// 1.Success
        /// 2.Already exists
        /// 3.DAL Error
        /// </summary>
        public int AddProcess(string in_Id, string in_Priority)
        {
            /*** Check: if Relationship exists ***/
            DAL.Process_Goal_Process optPGP = new DAL.Process_Goal_Process();
            if (optPGP.Exists(in_Id, this._id))
                return 2;

            Model.Process_Goal_Process myPGP = new Model.Process_Goal_Process();
            myPGP.processID = in_Id;
            myPGP.goal_Process = this._id;
            myPGP.priority = in_Priority;

            bool ok = optPGP.Add(myPGP);
            if (!ok) return 3;
            else return 1;
        }

        /// <summary>
        /// Remove a process to the GoalProcess
        /// 1.Success
        /// 2.Doesn't exist such Process
        /// 3.DAL Error
        /// </summary>
        public int RemoveProcess(string in_Id)
        {
            /*** Check: if Relationship exists ***/
            DAL.Process_Goal_Process optPGP = new DAL.Process_Goal_Process();
            if (!optPGP.Exists(in_Id, this._id))
                return 2;

            bool ok=optPGP.Delete(in_Id, this._id);
            if (!ok) return 3;
            return 1;
        }
    }
}
